-- Defining sort

quick is package{

  quick has type (list of %s,((%s,%s) =>boolean)) => list of %s;
  quick(list{X},_) is list{X};
  quick(list{},_) is list{};
   
  quick(L,C) where size(L)>1 is let{
    var lftHalf := list{};
    rgtHalf has type ref list of %s;
    var rgtHalf := list{};
    pivot is L[0];
    
    split has type action();
    split() do {
	  -- logMsg(info,"pivot is $pivot");
      for Ix->el in L do
      {
        if Ix>0 then{
          if C(el,pivot) then
            lftHalf := lftHalf<>list{el} -- list concatenate
          else
            rgtHalf := rgtHalf<>list{el};
        };
	    -- logMsg(info,"left half is $lftHalf");
        -- logMsg(info,"right half is $rgtHalf");
      }
    };
  } in valof{
      split();
      valis quick(lftHalf,C)<>list{pivot;..quick(rgtHalf,C)};
    }
}
